<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>维修上报</title>
    <!-- 引入Layui CSS -->
    <link rel="stylesheet" href="../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
    <script src="../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
    <style>
        .form-container {
            width: 600px;
            margin: 50px auto;
            padding: 20px;
            background-color: #fff;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        .form-title {
            text-align: center;
            font-size: 20px;
            color: #333;
            margin-bottom: 20px;
            padding-bottom: 10px;
            border-bottom: 1px solid #eee;
        }
        .layui-form-item {
            margin-bottom: 15px;
        }
    </style>
</head>
<body class="layui-bg-gray">
<div class="form-container">
    <div class="form-title">维修上报</div>
    <form class="layui-form" lay-filter="repairForm">
        <!-- 报修描述 -->
        <div class="layui-form-item">
            <label class="layui-form-label">报修描述</label>
            <div class="layui-input-block">
                    <textarea name="description" placeholder="请详细描述需要维修的内容"
                              class="layui-textarea" lay-verify="required"></textarea>
            </div>
        </div>

        <!-- 提交人姓名 -->
        <div class="layui-form-item">
            <label class="layui-form-label">姓名</label>
            <div class="layui-input-block">
                <input type="text" name="name" placeholder="请输入您的姓名"
                       class="layui-input" lay-verify="required">
            </div>
        </div>

        <!-- 优先级选择 -->
        <div class="layui-form-item">
            <label class="layui-form-label">优先级</label>
            <div class="layui-input-block">
                <select name="priority" id="priorities" lay-verify="required">
                    <option value="">请选择报修优先级</option>
<!--                    <option value="1">低</option>-->
<!--                    <option value="2">中</option>-->
<!--                    <option value="3">高</option>-->
                </select>
            </div>
        </div>

        <!-- 状态（默认待处理，用户不可修改） -->
        <div class="layui-form-item">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-block">
                <select name="status" disabled>
                    <option value="0" selected>待处理</option>
                </select>
                <input type="hidden" name="status" value="0">
            </div>
        </div>

        <!-- 提交按钮 -->
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="submitRepair">提交报修</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>


<script>
    // 初始化Layui组件
    layui.use(['form', 'layer',"jquery"], function() {
        var form = layui.form;
        var layer = layui.layer;
        var $ = layui.jquery;

        $.ajaxSetup({
            beforeSend: function(xhr) {
                const data = JSON.parse(localStorage.getItem('user'));
                //输出token
                console.log(data.token);
                if (data.token) {
                    xhr.setRequestHeader('Authorization', 'Bearer ' + data.token);
                }
            }
        });

        // 加载下拉框
        function loadSelectOptions(url, selectId) {
            $.getJSON(url, function (res) {
                if (res.code === 0 && res.data) {
                    var options = '';
                    res.data.forEach(function (item) {
                        options += `<option value="${item.id}">${item.value}</option>`;
                    });
                    $(selectId).append(options);
                    form.render('select');
                }
            });
        }
        loadSelectOptions('/apply/priorities', '#priorities')
        // 表单提交事件
        form.on('submit(submitRepair)', function(data) {
            // 构造请求参数（与AddRequestDTO字段对应）
            var param = {
                description: data.field.description,
                name: data.field.name,
                priority: parseInt(data.field.priority),
                status: parseInt(data.field.status),
                // residentId需根据实际登录情况获取（这里假设从本地存储获取）
                residentId: parseInt(localStorage.getItem('residentId'))||1
            };

            // 发送请求到后端
            $.ajax({
                url: '/apply/add',  // 后端接口地址
                type: 'POST',
                contentType: 'application/json',
                data: JSON.stringify(param),
                dataType: 'json',
                success: function(res) {
                    if (res.code === 0) {  // 假设成功状态码为0
                        layer.msg('报修提交成功', {icon: 1}, function() {
                            // 关闭当前页面或刷新
                            // window.location.href = '/repair/list';  // 跳转至报修列表页
                        });
                    } else {
                        layer.msg('提交失败：' + res.msg, {icon: 2});
                    }
                },
                error: function() {
                    layer.msg('网络异常，请稍后重试', {icon: 2});
                }
            });

            return false;  // 阻止表单默认提交
        });
    });
</script>
</body>
</html>
